<template>
  <van-button :loading=" isFollowed" v-if="!isfolloed" @click="clickBtn" class=" follow-btn" type="info" color="#3296fa"
    round size="small" icon="plus">
    关注
  </van-button>
  <van-button v-else @click="clickBtn" class="foolow-btn" round size="small">取消关注
  </van-button>
</template>

<script>
import { addUserfollwed, deleteUserfollwed } from '@/api/users.js'
export default {
  props: {
    // 父传子的值
    isfolloed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: Number,
      required: true
    }
  },
  model: {
    // 属性
    prop: 'isfolloed',
    event: 'updataLis'
  },
  data () {
    return {
      // 加载状态
      isFollowed: false
    }
  },
  methods: {
    async clickBtn (userId) {
      this.isFollowed = true
      try {
        if (this.isFollowed) {
          await deleteUserfollwed(this.autId)
          this.$toast('取关成功')
        } else {
          await addUserfollwed(userId)
          this.$toast('关注成功')
        }
        this.$emit('updataLis', !this.isfolloed)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail('不能关住自己')
        }
        this.$toast('操作失败')
      }
      this.isFollowed = false
    }
  }
}
</script>

<style>
</style>
